package com.portlet.credit_card;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.Company;
import com.liferay.portal.service.CompanyLocalServiceUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.util.Encryptor;
import com.liferay.util.EncryptorException;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.payment.validation.CardValidation;
import com.slayer.model.CreditCard;
import com.slayer.model.impl.CreditCardImpl;
import com.slayer.service.CreditCardLocalServiceUtil;

public class MyCreditCard extends MVCPortlet {

	public void saveAccountDetails(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException,
			PortletException, SystemException, PortalException {
		long cardId = 0l;
		String encryptedCardNumber = "";
		String encryptedCardCVV = "";
		boolean updateAll = true;

		ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest
				.getAttribute(WebKeys.THEME_DISPLAY);
		ArrayList<Long> myCardIdList = new ArrayList<Long>();

		List<CreditCard> updateAllEntries = new ArrayList<CreditCard>();

		List<CreditCard> creditCards = CreditCardLocalServiceUtil
				.getByUserId_CompanyId(themeDisplay.getUserId(),
						themeDisplay.getCompanyId());

		int[] ccListIndexes = StringUtil.split(
				ParamUtil.getString(actionRequest, "ccListIndexes"), 0);

		for (int i = 1; i <= ccListIndexes.length; i++) {

			int accListIndex = ccListIndexes[i - 1];
			cardId = ParamUtil.getLong(actionRequest, "cardId" + accListIndex);
			String cardFirstName = ParamUtil.getString(actionRequest,
					"cardFirstName" + accListIndex);
			String cardLastName = ParamUtil.getString(actionRequest,
					"cardLastName" + accListIndex);
			String cardNumber = ParamUtil.getString(actionRequest, "cardNumber"
					+ accListIndex);
			String CVV = ParamUtil.getString(actionRequest, "CVV"
					+ accListIndex);
			String expMM = ParamUtil.getString(actionRequest, "expMM"
					+ accListIndex);
			String expYY = ParamUtil.getString(actionRequest, "expYY"
					+ accListIndex);
			int defaultEntry = ParamUtil.getInteger(actionRequest,
					"defaultEntry");

			if (!CardValidation.isEmptyCard(actionRequest, cardFirstName,
					cardLastName, cardNumber, CVV, expMM, expYY)) {
				updateAll = false;
				break;
			}

			try {
				Company company = CompanyLocalServiceUtil
						.getCompanyById(themeDisplay.getCompanyId());
				encryptedCardNumber = Encryptor.encrypt(company.getKeyObj(),
						cardNumber);
				encryptedCardCVV = Encryptor.encrypt(company.getKeyObj(), CVV);
			} catch (EncryptorException e) {
				e.printStackTrace();
			} catch (PortalException e) {
				e.printStackTrace();
			}

			CreditCard card = updateCardItem(i, actionRequest,
					updateAllEntries, myCardIdList, creditCards, themeDisplay,
					cardId, cardFirstName, cardLastName, encryptedCardNumber,
					encryptedCardCVV, expMM, expYY, defaultEntry);

			updateAllEntries.add(card);
			myCardIdList.add(card.getCardId());

		}
		if (updateAll) {
			updateAll(updateAllEntries, myCardIdList, ccListIndexes,
					creditCards);
		}

	}

	private CreditCard updateCardItem(int i, ActionRequest actionRequest,
			List<CreditCard> updateAllEntries, ArrayList<Long> myCardIdList,
			List<CreditCard> creditCards, ThemeDisplay themeDisplay,
			long cardId, String cardFirstName, String cardLastName,
			String cardNumber, String CVV, String expMM, String expYY,
			int defaultEntry) throws SystemException, PortalException {
		CreditCard creditCard = null;

		if (creditCards == null || creditCards.isEmpty()) {
			List<CreditCard> newList = new ArrayList<CreditCard>();
			newList.add(new CreditCardImpl());
			creditCards = newList;
		}

		if (cardId == 0) {
			cardId = CounterLocalServiceUtil.increment();
			creditCard = CreditCardLocalServiceUtil.createCreditCard(cardId);
			creditCard.setDateAdded(new java.util.Date());

		} else {
			creditCard = CreditCardLocalServiceUtil.getCreditCard(cardId);
			creditCard.setDateModified(new Date());
		}
		creditCard.setCardId(cardId);
		creditCard.setUserId(themeDisplay.getUser().getUserId());
		creditCard.setCompanyId(themeDisplay.getCompanyId());
		creditCard.setCardFirstName(cardFirstName);
		creditCard.setCardLastName(cardLastName);
		creditCard.setCardNumber(cardNumber);
		creditCard.setCardCVV(CVV);
		creditCard.setCardExpMonth(expMM);
		creditCard.setCardExpYear(expYY);
		creditCard.setDateModified(new java.util.Date());
		creditCard.setDefaultEntry(i == defaultEntry);
		return creditCard;
	}

	private void updateAll(List<CreditCard> updateAllEntries,
			ArrayList<Long> myCardIdList, int[] ccListIndexes,
			List<CreditCard> creditCards) {

		try {
			for (CreditCard card : updateAllEntries) {
				CreditCardLocalServiceUtil.updateCreditCard(card);
			}
			if (ccListIndexes.length == myCardIdList.size()) {
				removeCard(myCardIdList, creditCards);
			}
		} catch (SystemException e) {
			e.printStackTrace();
		}
	}

	private void removeCard(ArrayList<Long> myCardIdList,
			List<CreditCard> creditCards) throws SystemException {
		for (CreditCard creditCard : creditCards) {
			if (creditCards.size() != 1
					&& !myCardIdList.contains(creditCard.getCardId())) {
				CreditCardLocalServiceUtil.deleteCreditCard(creditCard);
			}
		}
	}
}
